﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;

namespace SaleManagement.Web.Pages
{
  public partial class RepositoryList : BasePage
  {
    #region Properties
    private string m_innerError = string.Empty;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        LoadRepositoryList();
      }
    }

    protected void gvRepository_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Save")
      {
        m_innerError = "Save|" + SaveRepository();
      }
      else if (para[0] == "Delete")
      {
        int id = 0;
        int.TryParse(para[1], out id);
        m_innerError = DeleteRepository(id);
      }

      LoadRepositoryList();
    }

    protected void gvRepository_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }

    protected void gvRepository_PageIndexChanged(object sender, EventArgs e)
    {
      LoadRepositoryList();
    }

    protected void gvRepository_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
    {
      if (e.DataColumn.Name == "Command")
      {
        ASPxGridView gvObject = sender as ASPxGridView;
        HtmlImage img = gvObject.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "imgEdit") as HtmlImage;
        if (img != null)
        {
          img.Attributes.Add("onclick", "onEdit(" + e.VisibleIndex + ")");
        }
      }
    }
    #endregion

    #region Methods
    private void LoadRepositoryList()
    {
      string type = cbxSearchedType.Value == null ? null : cbxSearchedType.Value.ToString();
      gvRepository.DataSource = new RepositoryService().Search(type);
      gvRepository.DataBind();
    }

    private string SaveRepository()
    {
      try
      {
        RepositoryService service = new RepositoryService();
        Repository repository = null;
        if (!string.IsNullOrEmpty(hfRepositoryId.Value))
        {
          int id = -1;
          int.TryParse(hfRepositoryId.Value, out id);
          repository = service.GetById(id);
          if (repository == null)
          {
            return "Không tìm thấy sản phẩm tương ứng";
          }

          repository.UpdatedDate = DateTime.Now;
          repository.UpdatedBy = CurrentUser.Username;
        }
        else
        {
          repository = new Repository();
          repository.CreatedBy = CurrentUser.Username;
          repository.CreatedDate = DateTime.Now;
        }

        repository.Type = cbxType.Value.ToString();
        repository.Name = Utilities.SafeString(txtName.Text);
        repository.Address = Utilities.SafeString(txtAddress.Text);
        repository.Description = Utilities.SafeString(mmDescription.Text);
        service.Save(repository);
      }
      catch (Exception ex)
      {
        WriteLog("RepositoryList:: SaveRepository", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }

      return string.Empty;
    }

    private string DeleteRepository(int id)
    {
      try
      {
        new RepositoryService().Delete(id);
        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("RepositoryList::DeleteRepository", ex);
        return "Bạn không thể xóa kho này.";
      }
    }
    #endregion
  }
}
